package com.baichang.taoshunda.controller.mobile;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.UserBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by ${wushenglu}
 */
@Controller
@RequestMapping(ControllerConstants.BANKCARD)
public class BankCardMCtronller extends BaseMobileController {
    @Autowired
    private UserBankService userBankService;

    /**
     * 修改用户的银行卡信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateBankCard")
    public void updateBankCard(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bankId = getInt(json, "bankId", 0);
                Integer bankTypeId = getInt(json, "bankTypeId", 0);
                String bankNo = getString(json, "bankNo", "");
                return userBankService.updateBankCard(bankId, bankTypeId, bankNo);
            }
        });
    }

    /**
     * 解绑银行卡
     *
     * @param request
     * @param response
     */
    @RequestMapping("/unBundling")
    public void unBundlingByUserId(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                Integer bankId = getInt(json, "bankId", 0);
                return userBankService.unBundlingByUserId(userId, bankId);
            }
        });
    }

    /**
     * 查询用户的默认银行卡
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findUserDefaultBankCard")
    public void findUserDefaultBankCard(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                return userBankService.findUserDefaultBankCard(userId);
            }
        });
    }

    /**
     * 提现
     *
     * @param request
     * @param response
     */
    @RequestMapping("/cash")
    public void cash(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                //提现类型  1 普通提现   2 返利提现
                Integer type = getInt(json, "type", 2);
                Integer bankCardId = getInt(json, "bankCardId", 0);
                Double money = getDouble(json, "money", 0D);
                return userBankService.cash(userId, bankCardId, money, type);
            }
        });
    }

    /**
     * 检查银行卡是否绑定
     * disId     商家编号
     * cardTypeId 银行编号
     * cardNumber 银行卡号
     */
    @RequestMapping("/checkBundling")
    public void checkBundling(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer disId = getInt(json, "disId", 0);
                Integer bankId = getInt(json, "cardTypeId", 0);
                String cardNumber = getString(json, "cardNumber", "");
                return userBankService.checkBundlingByDisId(disId, bankId, cardNumber);
            }
        });
    }

    /**
     * 查看用户的银行卡信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findUserBankCard")
    public void findUserBankCard(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                return userBankService.findUserBankCard(userId);
            }
        });
    }

    /**
     * 添加配送员银行卡
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addUserBankCard")
    public void addUserBankCard(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                Integer cardTypeId = getInt(json, "cardTypeId", 0);
                String cardNumber = getString(json, "cardNumber", "");
                String userName = getString(json, "userName", "");
                return userBankService.addUserBankCard(userId, cardTypeId, cardNumber, userName);
            }
        });
    }

    /**
     * 查看开户行列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findBankList")
    public void findBankList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                return userBankService.findBank();
            }
        });
    }

}
